<?php 
/**
 * 获取路径下的所有控制器名
 * @param  [type] $dir [description]
 * @return [type]      [description]
 */
function getControllers($dir){
	$pathList = glob($dir . '/*.php');
    $res = [];
    foreach($pathList as $key => $value) {
        $res[] = basename($value, '.php');
    }
    $bl = in_array('Base',$res);
	if( $bl ){
		$key = array_search('Base', $res);
		if( $key !== false ){
			array_splice($res, $key, 1);
		}
	}
    return $res;
}

/**
 * 获取路径下的所有方法名
 * @param  [type] $dir [description]
 * @return [type]      [description]
 */
function getMethods($dir){
	$file = $dir . '.php';
	if (file_exists($file)) {
		$content = file_get_contents($file);
	    preg_match_all("/.*?public.*?function(.*?)\(.*?\)/i", $content, $matches);
	    $functions = $matches[1];
	    //排除部分方法
	    $inherents_functions = array('_initialize', '__construct', 'getActionName', 'isAjax', 'display', 'show', 'fetch', 'buildHtml', 'assign', '__set', 'get', '__get', '__isset', '__call', 'error', 'success', 'ajaxReturn', 'redirect', '__destruct', '_empty');
	    foreach ($functions as $func) {
	        $func = trim($func);
	        if (!in_array($func, $inherents_functions)) {
	            $customer_functions[] = $func;
	        }
	    }
	    return $customer_functions;
	}
	return null;
}

// 密码加密
// function encrypt($str){
//     return md5('brs'.$str);
// }

/**
 * 获取客户端的IP地址
 * @return [type] [description]
 */
function getIP(){            
    if (getenv("HTTP_CLIENT_IP"))
        $ip = getenv("HTTP_CLIENT_IP");
    else if(getenv("HTTP_X_FORWARDED_FOR"))
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    else if(getenv("REMOTE_ADDR"))
        $ip = getenv("REMOTE_ADDR");
    else $ip = "Unknow";

    if(preg_match('/^((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1 -9]?\d))))$/', $ip))          
        return $ip;
    else
        return '';
}

/**
 * 添加管理员操作日志
 * @param [type] $title [description]
 */
function addAdminLog($title){
	$add['admin_id'] = Session::get('admin_id','brscms');
	$add['title'] = $title;
	$add['ip'] = getIP();
	$add['create_time'] = time();
	db('admin_log')->insert($add);
}